/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.JiuWechatMember;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.dao.JiuWechatMemberDao;

/**
 * 微信会员Service
 * @author sxh
 * @version 2018-08-15
 */
@Component
@Transactional(readOnly = true)
public class JiuWechatMemberService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(JiuWechatMemberService.class);
	
	@Autowired
	private JiuWechatMemberDao jiuWechatMemberDao;
	
	public JiuWechatMember get(Long id) {
		return jiuWechatMemberDao.findOne(id);
	}
	
	public Page<JiuWechatMember> find(Page<JiuWechatMember> page, JiuWechatMember jiuWechatMember) {
		DetachedCriteria dc = jiuWechatMemberDao.createDetachedCriteria();
		dc.createAlias("user", "user");
		if (jiuWechatMember.getUser()!=null &&jiuWechatMember.getUser().getId()!=null &&jiuWechatMember.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", jiuWechatMember.getUser().getId()));
		}
		
		if (jiuWechatMember.getUser()!=null && StringUtils.isNotEmpty(jiuWechatMember.getUser().getName())){
			dc.add(Restrictions.like("user.name", "%"+jiuWechatMember.getUser().getName()+"%"));
		}
		if (jiuWechatMember.getId() !=null && jiuWechatMember.getId()>0){
			dc.add(Restrictions.eq("id", jiuWechatMember.getId()));
		}
		if (StringUtils.isNotEmpty(jiuWechatMember.getName())){
			dc.add(Restrictions.like("name", "%"+jiuWechatMember.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(jiuWechatMember.getMobile())){
			dc.add(Restrictions.like("mobile", "%"+jiuWechatMember.getMobile()+"%"));
		}
		if (StringUtils.isNotEmpty(jiuWechatMember.getNumber())){
			dc.add(Restrictions.like("number", "%"+jiuWechatMember.getNumber()+"%"));
		}
		if(jiuWechatMember.getStartTime()!=null){
			String timeField = "createTime";
			dc.add(Restrictions.ge(timeField,jiuWechatMember.getStartTime() ));
		}
		
		if(jiuWechatMember.getEndTime() !=null){
			String timeField = "createTime";
			dc.add(Restrictions.le(timeField,jiuWechatMember.getEndTime()));
		}
		dc.add(Restrictions.eq("delFlag", JiuWechatMember.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return jiuWechatMemberDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(JiuWechatMember jiuWechatMember) {
		jiuWechatMemberDao.save(jiuWechatMember);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		jiuWechatMemberDao.deleteById(id);
	}
   
	/**
	 * 
	 * @function: <p> 根据会员的openid和用户id查询</p>
	 * @param parseLong
	 * @param openId
	 * @return
	 */
	public JiuWechatMember getByUserIdAndOpenId(long userId, String openId) {
		// TODO Auto-generated method stub
		DetachedCriteria dc = jiuWechatMemberDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id",userId));
		if(StringUtils.isNotEmpty(openId)){
			dc.add(Restrictions.eq("openId",openId));
		}
		List<JiuWechatMember> list=jiuWechatMemberDao.find(dc);
		return list.size() > 0 ? list.get(0) : null;
	}

	/**
	 * 
	 * @function: <p>根据会员的openId获得会员的信息 </p>
	 * @param openId
	 * @return
	 */
	public JiuWechatMember getByOpenId(String openId) {
		// TODO Auto-generated method stub
		DetachedCriteria dc = jiuWechatMemberDao.createDetachedCriteria();
		if(StringUtils.isNotEmpty(openId)){
			dc.add(Restrictions.eq("openId",openId));
		}
		List<JiuWechatMember> list=jiuWechatMemberDao.find(dc);
		return list.size() > 0 ? list.get(0) : null;
	}

	/**
	 * 
	 * @function: <p> 根据会员的手机号获得会员</p>
	 * @param mobile
	 * @return
	 */
	public JiuWechatMember getByMobile(String mobile) {
		// TODO Auto-generated method stub
		DetachedCriteria dc = jiuWechatMemberDao.createDetachedCriteria();
		if(StringUtils.isNotEmpty(mobile)){
			dc.add(Restrictions.eq("mobile",mobile));
		}
		List<JiuWechatMember> list=jiuWechatMemberDao.find(dc);
		return list.size() > 0 ? list.get(0) : null;
	}

	/**
	 * 
	 * @function: <p>根据会员的手机号和openId查询 </p>
	 * @param openId
	 * @param mobile
	 * @return
	 */
	public JiuWechatMember getByOpenIdAndMobile(String openId, String mobile) {
		// TODO Auto-generated method stub
		DetachedCriteria dc = jiuWechatMemberDao.createDetachedCriteria();
		if(StringUtils.isNotEmpty(openId)){
			dc.add(Restrictions.eq("openId",openId));
		}
		if(StringUtils.isNotEmpty(mobile)){
			dc.add(Restrictions.eq("mobile",mobile));
		}
		List<JiuWechatMember> list=jiuWechatMemberDao.find(dc);
		return list.size() > 0 ? list.get(0) : null;
	}
	
}
